from django.db import models

# Create your models here.


def ifNull(data):
    if data != "" and data is not None:
        return data
    else:
        return "..."


class ScanEnthic(models.Model):
    enthic_code = models.CharField(max_length=50, null=True, blank=True, verbose_name='民族编码')
    enthic_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='民族名称')
    enthic_status = models.BooleanField(default=True, verbose_name='是否使用')

    def __str__(self):
        if self.enthic_status is True:
            return str(self.enthic_name)
        else:
            return str(self.enthic_name) + '(不再使用)'

    class Meta:
        managed = True
        db_table = 'scan_enthic'
        verbose_name_plural = '民族表'
        verbose_name = '民族表'


class ScanNative(models.Model):
    native_code = models.CharField(max_length=50, null=True, blank=True, verbose_name='籍贯编码')
    native_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='籍贯名称')
    native_status = models.BooleanField(default=True, verbose_name='籍贯状态')

    def __str__(self):
        if self.native_status is True:
            return str(self.native_name)
        else:
            return str(self.native_name) + '(不再使用)'

    class Meta:
        managed = True
        db_table = 'scan_native'
        verbose_name_plural = '籍贯表'
        verbose_name = '籍贯表'


class ScanFirstDegree(models.Model):
    first_degree_code = models.CharField(max_length=50, null=True, blank=True, verbose_name='第一学历编码')
    first_degree_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='第一学历名称')

    def __str__(self):
        return str(self.first_degree_name)

    class Meta:
        managed = True
        db_table = 'scan_first_degree'
        verbose_name_plural = '第一学历表'
        verbose_name = '第一学历表'


'''员工附件类别'''


class ScanFilesType(models.Model):
    files_type_name = models.CharField(null=True, blank=True, max_length=50, verbose_name='文件类别名称')

    class Meta:
        managed = True
        db_table = 'scan_files_type'
        verbose_name_plural = '文件类别表'
        verbose_name = '文件类别表'


'''员工附件'''

emp_gender_choices = (
    (1, '男'),
    (2, '女'),
)
emp_approve_status_choices = (
    (1, '未审批'),
    (2, '已完成'),
    (3, '已驳回'),
)


class ScanEmployee(models.Model):
    emp_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='员工姓名')
    emp_code = models.CharField(max_length=50, null=True, blank=True, verbose_name='员工工号')
    emp_entry_date = models.DateTimeField(blank=True, null=True, verbose_name='集团入职日期')
    emp_dept = models.ForeignKey(to='employee.EmpDepartment', on_delete=models.SET_NULL, null=True, blank=True,
                                 verbose_name='人员部门', db_constraint=False)
    emp_post = models.ForeignKey(to='employee.EmpPosition', on_delete=models.SET_NULL, null=True, blank=True,
                                 verbose_name='人员岗位', db_constraint=False)
    emp_job_duty = models.ForeignKey(to='employee.EmpJobDuty', on_delete=models.SET_NULL, null=True, blank=True,
                                     verbose_name='人员职务', db_constraint=False)
    emp_job_class = models.ForeignKey(to='employee.EmpJobClass', on_delete=models.SET_NULL, null=True, blank=True,
                                      verbose_name='人员职等', db_constraint=False)
    emp_job_grade = models.ForeignKey(to='employee.EmpJobGrade', on_delete=models.SET_NULL, null=True, blank=True,
                                      verbose_name='人员职级', db_constraint=False)
    emp_job_rank = models.ForeignKey(to='employee.EmpJobRank', on_delete=models.SET_NULL, null=True, blank=True,
                                     verbose_name='人员合同归属', db_constraint=False)
    emp_ethnic = models.ForeignKey(to='scanQrCode.ScanEnthic', on_delete=models.SET_NULL, null=True, blank=True,
                                   verbose_name='人员民族', db_constraint=False)
    emp_native_place = models.ForeignKey(to='scanQrCode.ScanNative', on_delete=models.SET_NULL, null=True, blank=True,
                                         verbose_name='人员籍贯', db_constraint=False)
    emp_pay_type = models.ForeignKey(to='employee.EmpPayType', on_delete=models.SET_NULL, null=True, blank=True,
                                     verbose_name='人员记薪方式', db_constraint=False)
    emp_first_degree = models.ForeignKey(to='scanQrCode.ScanFirstDegree', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='第一学历', db_constraint=False)
    # emp_job_mgmt = models.ForeignKey(to='scanQrCode.S',on_delete=models.SET_NULL, null=True, blank=True, verbose_name='人员合同归属', db_constraint=False) # 管理归属在合同归属中
    emp_id_card = models.CharField(max_length=50, null=True, blank=True, verbose_name='员工身份证')
    emp_gender = models.IntegerField(blank=True, null=True, choices=emp_gender_choices, verbose_name='员工性别')
    emp_marriage = models.CharField(blank=True, null=True, max_length=50, verbose_name='员工婚姻状况')
    emp_birthday = models.DateTimeField(blank=True, null=True, verbose_name='员工出生日期')
    emp_phone = models.CharField(max_length=50, null=True, blank=True, verbose_name='员工电话号码')
    emp_grade = models.CharField(max_length=50, null=True, blank=True, verbose_name='员工等级')
    emp_dl_idl = models.CharField(max_length=50, null=True, blank=True, verbose_name='DL/IDL')
    emp_labour_source = models.CharField(max_length=50, null=True, blank=True, verbose_name='劳务来源')
    emp_political_affiliation = models.CharField(max_length=50, null=True, blank=True, verbose_name='政治面貌')
    emp_nationality = models.CharField(max_length=50, null=True, blank=True, verbose_name='国籍')
    emp_domicile_address = models.CharField(max_length=50, null=True, blank=True, verbose_name='户籍地址')
    emp_latest_address = models.CharField(max_length=50, null=True, blank=True, verbose_name='现住址')
    emp_urgent_person = models.CharField(max_length=50, null=True, blank=True, verbose_name='紧急联系人')
    emp_urgent_phone = models.CharField(max_length=50, null=True, blank=True, verbose_name='紧急联系人电话')
    emp_urgent_relation = models.CharField(max_length=50, null=True, blank=True, verbose_name='关系')
    emp_gradute_time = models.DateTimeField(blank=True, null=True, verbose_name='毕业时间')
    emp_gradute_insituation = models.CharField(max_length=50, null=True, blank=True, verbose_name='毕业院校')
    emp_major = models.CharField(max_length=50, null=True, blank=True, verbose_name='所选专业')
    emp_educate_method = models.CharField(max_length=50, null=True, blank=True, verbose_name='教育方式')
    emp_bank_card_num = models.CharField(max_length=50, null=True, blank=True, verbose_name='银行卡号')
    emp_bank = models.CharField(max_length=50, null=True, blank=True, verbose_name='开户行')
    emp_create_time = models.DateTimeField(blank=True, null=True, auto_now_add=True, verbose_name='创建时间')
    emp_modifiy_time = models.DateTimeField(blank=True, null=True, auto_now=True, verbose_name='修改时间')
    emp_predict_entry_date = models.DateTimeField(blank=True, null=True, verbose_name='预入职日期')
    emp_if_fill_in = models.BooleanField(default=False, verbose_name='是否填写')
    emp_approve_status = models.IntegerField(default=1, verbose_name='人员状态', choices=emp_approve_status_choices)

    def short_entry_time(self):
        if self.emp_predict_entry_date is None:
            return '-'
        else:
            return str(self.emp_predict_entry_date)[:10]
    # 图片显示
    class Meta:
        managed = True
        db_table = 'scan_employee'
        verbose_name_plural = '员工表'
        verbose_name = '员工表'


'''文件表'''


class ScanFiles(models.Model):
    files_person = models.ForeignKey(to='scanQrCode.ScanEmployee', on_delete=models.CASCADE, null=True, blank=True, verbose_name='人员id', db_constraint=False)
    files_type = models.ForeignKey(to='scanQrCode.ScanFilesType', on_delete=models.CASCADE, null=True, blank=True, verbose_name='文件类别', db_constraint=False)
    files_url = models.CharField(null=True, blank=True, max_length=255, verbose_name='文件')
    files_status = models.BooleanField(default=True, verbose_name='文件状态')
    files_create_time = models.DateTimeField(blank=True, null=True, auto_now_add=True, verbose_name='创建时间')
    files_modifiy_time = models.DateTimeField(blank=True, null=True, auto_now=True, verbose_name='修改时间')

    class Meta:
        managed = True
        db_table = 'scan_files'
        verbose_name_plural = '文件表'
        verbose_name = '文件表'
